home *** CD-ROM | disk | FTP | other *** search
/ CD/PC Actual 76 / DVD Actual 1 Marzo 2003.iso / Trial / TurboCAD 7.1 Pro / Data.Cab / F25097_TDialog.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-11-10  |  21.0 KB  |  1,007 lines

  1. // TDialog.cpp : implementation file
  2. //
  3. #include <afxwin.h>
  4. #include "stdafx.h"
  5. #include "InsTool.h"
  6. #include "InsSymb.h"
  7. #include "ViewWnd.h"
  8. #include "TDialog.h"
  9. #include "TDialog1.h"
  10. #include "Dirdialog.h"
  11.  
  12. #ifdef _DEBUG
  13. #define new DEBUG_NEW
  14. #undef THIS_FILE
  15. static char THIS_FILE[] = __FILE__;
  16. #endif
  17.  
  18. /*
  19. const IID LIBID_IMSIGXLib = {0x6A481400,0xE531,0x11CF,{0xA1,0x15,0x00,0xA0,0x24,0x15,0x8D,0xAF}};
  20. const IID IID_PickResult = {0x6A481123,0xE531,0x11CF,{0xA1,0x15,0x00,0xA0,0x24,0x15,0x8D,0xAF}};
  21. //const IID IID_Tool = {0x6A48112C,0xE531,0x11CF,{0xA1,0x15,0x00,0xA0,0x24,0x15,0x8D,0xAF}};
  22. const IID IID_Graphics = {0x6A48110A, 0xE531, 0x11CF, {0xA1,0x15,0x00,0xA0,0x24,0x15,0x8D,0xAF}};
  23. const IID IID_View = {0x6A481110, 0xE531, 0x11CF, {0xA1,0x15,0x00,0xA0,0x24,0x15,0x8D,0xAF}};
  24. const IID IID_Window = {0x6A481124, 0xE531, 0x11CF, {0xA1,0x15,0x00,0xA0,0x24,0x15,0x8D,0xAF} };
  25. const CLSID CLSID_Application  = {0x6A481001,0xE531,0x11CF,{0xA1,0x15,0x00,0xA0,0x24,0x15,0x8D,0xAF}};
  26. const CLSID CLSID_XGraphic  = {0x6A481803,0xE531,0x11CF,{0xA1,0x15,0x00,0xA0,0x24,0x15,0x8D,0xAF}};
  27. static const IID IID_IAppEvents = { 0x6A481301, 0xE531, 0x11CF, { 0xA1, 0x15, 0x0, 0xA0, 0x24, 0x15, 0x8D, 0xAF } };
  28. */
  29.  
  30. /////////////////////////////////////////////////////////////////////////////
  31. BEGIN_MESSAGE_MAP(CTDialog, CDialog)
  32.     //{{AFX_MSG_MAP(CTDialog)
  33.     ON_BN_CLICKED(ID_CLOSE, OnClose)
  34.     ON_BN_CLICKED(IDC_BROWSE, OnBrowse)
  35.     ON_BN_CLICKED(IDC_MODIFY, OnModify)
  36.     ON_BN_CLICKED(IDC_CHECK1, OnCheck1)
  37.     ON_WM_SYSCOMMAND()
  38.     //}}AFX_MSG_MAP
  39. END_MESSAGE_MAP()
  40.  
  41. BEGIN_DISPATCH_MAP(CTDialog, CDialog)
  42.     //{{AFX_DISPATCH_MAP(CTDialog)
  43.         // NOTE - the ClassWizard will add and remove mapping macros here.
  44.     DISP_FUNCTION(CTDialog, "MouseDown", MouseDown, VT_EMPTY, VTS_DISPATCH VTS_DISPATCH VTS_DISPATCH VTS_I2 VTS_I4 VTS_I4 VTS_I4 VTS_PBOOL)
  45.     DISP_FUNCTION(CTDialog, "MouseUp", MouseUp, VT_EMPTY, VTS_DISPATCH VTS_DISPATCH VTS_DISPATCH VTS_I2 VTS_I4 VTS_I4 VTS_I4 VTS_PBOOL)
  46.     DISP_FUNCTION(CTDialog, "MouseMove", MouseMove, VT_EMPTY, VTS_DISPATCH VTS_DISPATCH VTS_DISPATCH VTS_I4 VTS_I4 VTS_I4 VTS_PBOOL)
  47. //    DISP_FUNCTION(CTDialog,    "DrawingBeforeClose", DrawingBeforeClose, VT_EMPTY, VTS_DISPATCH VTS_DISPATCH VTS_DISPATCH VTS_I4 VTS_I4 VTS_I4 VTS_PBOOL)
  48.     DISP_FUNCTION(CTDialog,    "DrawingBeforeClose", DrawingBeforeClose, VT_EMPTY, VTS_DISPATCH VTS_PBOOL)
  49.     
  50.     //}}AFX_DISPATCH_MAP
  51.  
  52. END_DISPATCH_MAP()
  53.  
  54. // Note: we add support for IID_ITDialog to support typesafe binding
  55. //  from VBA.  This IID must match the GUID that is attached to the 
  56. //  dispinterface in the .ODL file.
  57.  
  58. // {0416A816-839B-11D2-82F6-006052012837}
  59. ///static const IID IID_ITDialog =
  60. ///{ 0x416a816, 0x839b, 0x11d2, { 0x82, 0xf6, 0x0, 0x60, 0x52, 0x1, 0x28, 0x37 } };
  61.  
  62. BEGIN_INTERFACE_MAP(CTDialog, CDialog)
  63.     INTERFACE_PART(CTDialog, IID_IAppEvents, Dispatch)
  64. END_INTERFACE_MAP()
  65.  
  66. /////////////////////////////////////////////////////////////////////////////
  67.  
  68.  
  69.  
  70. // CTDialog dialog
  71.  
  72.  
  73. CTDialog::CTDialog(CInsSymb* pTool, CWnd* pParent, /*=NULL*/Tool *pThisTool)
  74.     : CDialog(CTDialog::IDD, pParent),
  75.     m_dwEventConnection(0),
  76.     m_dwEventMask(~0UL),
  77.     m_pIApplication(NULL),
  78.     m_FirstClick(FALSE),
  79.     m_bDragging(FALSE),
  80.     m_pDragGraphic(NULL),
  81.     m_pView(NULL),
  82.     m_pThisTool(NULL),
  83.     m_wndView(pTool)
  84.  
  85. {
  86.     EnableAutomation();
  87.  
  88.     //{{AFX_DATA_INIT(CTDialog)
  89.         // NOTE: the ClassWizard will add member initialization here
  90.     //}}AFX_DATA_INIT
  91.  
  92.     m_pTool = pTool;
  93.     m_pTool->AddRef();
  94.  
  95.     m_pTool->m_bRunned = TRUE;
  96.  
  97.     m_pThisTool = pThisTool;
  98.     m_pThisTool->AddRef();
  99. }
  100.  
  101. /*
  102. void CTDialog::OnFinalRelease()
  103. {
  104.     // When the last reference for an automation object is released
  105.     // OnFinalRelease is called.  The base class will automatically
  106.     // deletes the object.  Add additional cleanup required for your
  107.     // object before calling the base class.
  108.  
  109.     CDialog::OnFinalRelease();
  110. }
  111. */
  112. void CTDialog::DoDataExchange(CDataExchange* pDX)
  113. {
  114.     CDialog::DoDataExchange(pDX);
  115.     //{{AFX_DATA_MAP(CTDialog)
  116.         // NOTE: the ClassWizard will add DDX and DDV calls here
  117.     //}}AFX_DATA_MAP
  118. }
  119.  
  120.  
  121. // CTDialog message handlers
  122. BOOL CTDialog::OnInitDialog() 
  123. {
  124.     
  125.     BSTR bstrName;
  126.     CString cstrPropName;
  127.     CString cstrInitPath;
  128.     COleVariant varItem;
  129.     COleVariant varVal;
  130.     Properties *pProps = NULL;
  131.     Property *pProp = NULL;
  132.     HRESULT hRes = E_FAIL;
  133.     BOOL ret = FALSE;
  134.  
  135.     CDialog::OnInitDialog();
  136.     m_list.SubclassDlgItem( IDC_LIST1 , this );
  137.     CToolList *pList = (CToolList *)GetDlgItem(IDC_LIST1);
  138.     
  139.     try
  140.     {
  141.  
  142. /*
  143.         hRes = m_pThisTool->get_Application(&m_pIApplication);
  144.         CHECK_HRESULT(hRes)
  145.         hRes = m_pIApplication->get_Properties(&pProps);
  146.         CHECK_HRESULT(hRes)
  147.  
  148.  
  149.         cstrPropName = "$SYMLIBPATH";
  150.                     
  151.         bstrName = cstrPropName.AllocSysString();
  152.         ::VariantInit(&varItem);
  153.         varItem.vt = VT_BSTR;
  154.         varItem.bstrVal = bstrName;
  155.  
  156.         hRes =pProps->get_Item (&varItem, &pProp);
  157.         CHECK_HRESULT(hRes);
  158.  
  159.         varVal.vt = VT_BSTR;
  160.  
  161.         hRes = pProp->get_Value (NULL, &varVal);
  162.         CHECK_HRESULT(hRes);
  163.  
  164.         cstrInitPath = varVal.bstrVal;
  165.  
  166.         if (cstrInitPath.IsEmpty ())
  167.         {
  168.             cstrInitPath = "c\\";
  169.         
  170.         }
  171. */        
  172.  
  173.         HMODULE hHdl = NULL;
  174.         char path[256];
  175.  
  176.         DWORD dBufSize = 256;
  177.         DWORD dW = 0; 
  178.  
  179.         hHdl = GetModuleHandle(NULL);
  180.  
  181.         dW = GetModuleFileName(hHdl, path, dBufSize);  
  182.         hHdl = NULL;
  183.  
  184.         cstrInitPath = path;
  185.         cstrInitPath = cstrInitPath.Left(cstrInitPath.GetLength () - 10); // delete module name from path
  186.  
  187. //        cstrInitPath = "c:\\";
  188.         pList->RefreshFileList(cstrInitPath);
  189.  
  190.         CEdit *pPath = (CEdit *) GetDlgItem(IDC_EDITPATH);
  191.         pPath->SetWindowText(cstrInitPath);
  192.         CString selected;
  193.  
  194.         
  195.         m_wndView.SubclassDlgItem(IDC_PREVIEW, this);
  196.         CViewWnd *pView = (CViewWnd *)GetDlgItem(IDC_PREVIEW);
  197.     //    ConnectEvents();
  198.         if(pList->SetCurSel(0) != LB_ERR)
  199.         {
  200.  
  201.             pList->GetText(0 ,(pList->m_cstrCurSelected));
  202.  
  203.             pView->CreatePreview();
  204.             pView->DoPreview();            
  205.             ConnectEvents();
  206.         
  207.         }
  208.         else
  209.         {
  210.             ConnectEvents();
  211.         }
  212.         ret = TRUE;
  213.     }
  214.  
  215.     catch (...)
  216.     {
  217.         TRACE_EXCEPTION("CTDialog::OnInitDialog")
  218.         _clearfp();
  219.     }
  220. //    RELEASE(pProp)
  221. //    RELEASE(pProps)
  222. //    RELEASE(m_pIApplication)
  223.     // TODO: Add extra initialization here
  224.     
  225.     return ret;  // return TRUE unless you set the focus to a control
  226.                   // EXCEPTION: OCX Property Pages should return FALSE
  227. }
  228.  
  229. void CTDialog::OnClose() 
  230. {
  231.     // TODO: Add your message handler code here and/or call default
  232.     if(m_dwEventConnection != 0)
  233.         DisconectEvents();
  234.  
  235.     CViewWnd *pView = (CViewWnd *)GetDlgItem(IDC_PREVIEW);
  236.     pView->ClearAll();
  237.  
  238.     m_pTool->m_bRunned = FALSE;
  239.     if(m_pTool != NULL)
  240.     {
  241.         m_pTool->Release();
  242.         m_pTool = NULL;
  243.         
  244.     }
  245.     if(m_pThisTool != NULL)
  246.     {
  247.         m_pThisTool->Release();
  248.         m_pThisTool = NULL;
  249.         
  250.     }
  251.  
  252.     CDialog::OnClose();
  253.     DestroyWindow();
  254.     
  255. }
  256.  
  257. void CTDialog::OnSysCommand(UINT nID, LPARAM lParam )
  258. {
  259.     if (nID == SC_CLOSE)
  260.     {
  261.         if(m_dwEventConnection != 0)
  262.         DisconectEvents();
  263.     
  264.         CViewWnd *pView = (CViewWnd *)GetDlgItem(IDC_PREVIEW);
  265.         pView->ClearAll();
  266.         m_pTool->m_bRunned = FALSE;
  267.         if(m_pTool != NULL)
  268.         {
  269.             m_pTool->Release();
  270.             m_pTool = NULL;
  271.         
  272.         }
  273.         if(m_pThisTool != NULL)
  274.         {
  275.             m_pThisTool->Release();
  276.             m_pThisTool = NULL;
  277.         
  278.         }
  279.  
  280.     }
  281.     CDialog::OnSysCommand(nID, lParam );
  282.  
  283. }
  284.  
  285. void CTDialog::OnModify() 
  286. {
  287.     // TODO: Add your control notification handler code here
  288.         CWnd *pMainWnd = AfxGetMainWnd();
  289.         CTDialog1 *pdlg1 = new CTDialog1(m_pTool, pMainWnd);
  290.         int i =    pdlg1->DoModal();
  291.  
  292.  
  293. }
  294.  
  295. void CTDialog::OnCheck1() 
  296. {
  297.     // TODO: Add your control notification handler code here
  298.     
  299.     CViewWnd *pView = (CViewWnd *)GetDlgItem(IDC_PREVIEW);
  300.     AFX_MANAGE_STATE(m_pTool->m_pTCState);
  301.  
  302.  
  303.     Properties *pProps = NULL;
  304.     Property *pProp = NULL;
  305.     VARIANT varVal;//(0L);
  306.     varVal.vt = VT_I2;
  307.  
  308.     CString cstrName("TileMode");//# Non-localizable string#
  309.     BSTR bstrName = cstrName.AllocSysString();
  310.  
  311.     VARIANT varItem;
  312.   ::VariantInit(&varItem);
  313.     varItem.vt = VT_BSTR;
  314.     varItem.bstrVal = bstrName;
  315.     try
  316.     {
  317.  
  318.         if(pView->m_pPreviewDrawing != NULL)
  319.         {
  320.             HRESULT hRes = pView->m_pPreviewDrawing->get_Properties(&pProps);
  321.             CHECK_HRESULT(hRes)
  322.  
  323.             hRes = pProps->get_Item(&varItem, &pProp);
  324.             CHECK_HRESULT(hRes)
  325.  
  326.             pProp->get_Value(NULL, &varVal);
  327.  
  328.             if (varVal.iVal == 0) // cur drawing mode is paper space
  329.             {
  330.                 varVal.iVal = 1;
  331.                 hRes = pProp->put_Value(NULL, &varVal);
  332.                 CHECK_HRESULT(hRes)
  333.                     
  334.             }
  335.             else  // cur drawing mode is model space
  336.             {
  337.                 varVal.iVal = 0;
  338.                 hRes = pProp->put_Value(NULL, &varVal); // switch the preview drawing to paper space
  339.                 CHECK_HRESULT(hRes)
  340.  
  341.             }
  342.             
  343.             pView->Invalidate();
  344.  
  345.             hRes = pView->m_pPreviewView->ZoomToExtents();
  346.             CHECK_HRESULT(hRes)
  347.  
  348.             hRes = pView->m_pPreviewView->Refresh();
  349.             CHECK_HRESULT(hRes)
  350.             ::VariantClear(&varItem);
  351.         }
  352.  
  353.  
  354.     }
  355.     catch (...)
  356.     {
  357.         _clearfp();
  358.  
  359.         TRACE_EXCEPTION("TDialog::OnCheck1")
  360.     }
  361.     
  362.     if (pProp != NULL)
  363.     {
  364.         pProp->Release();
  365.         pProp = NULL;
  366.     }
  367.     if (pProps != NULL)
  368.     {
  369.         pProps->Release();
  370.         pProps = NULL;
  371.     }
  372.  
  373. }
  374. void CTDialog::DrawingBeforeClose(LPDISPATCH WhichDrawing, BOOL FAR* Cancel)
  375. {
  376.     *Cancel = FALSE;
  377.     Drawings *pDrs = NULL;
  378.     long n = 0;
  379.  
  380.     try
  381.     {
  382.         HRESULT hRes = m_pIApplication->get_Drawings(&pDrs);
  383.         CHECK_HRESULT(hRes)
  384.         hRes = pDrs->get_Count(&n);
  385.         CHECK_HRESULT(hRes)
  386.         if (n == 2)
  387.         {
  388.             if(m_dwEventConnection != 0)
  389.                 DisconectEvents();
  390.  
  391.             CViewWnd *m_wndView = (CViewWnd *)GetDlgItem(IDC_PREVIEW);
  392.             m_wndView->ClearAll();
  393.  
  394.             m_pTool->m_bRunned = FALSE;
  395.  
  396.             CDialog::OnClose();
  397.             DestroyWindow();
  398.         }
  399.  
  400.  
  401.     }
  402.  
  403.     catch (...)
  404.     {
  405.         _clearfp();
  406.  
  407.         TRACE_EXCEPTION("TDialog::DrawingBeforeClose")
  408.     }
  409.     if (pDrs != NULL)
  410.     {
  411.         pDrs->Release();
  412.         pDrs = NULL;
  413.     }
  414.  
  415.  
  416. }
  417. void CTDialog::MouseDown(LPDISPATCH WhichDrawing, LPDISPATCH WhichView, LPDISPATCH WhichWindow, short Button, long Shift, long X, long Y, IMSI_BOOL* pbCancel)
  418. {
  419.     *pbCancel = FALSE;
  420.  
  421. }
  422.  
  423. void CTDialog::MouseUp(LPDISPATCH WhichDrawing, LPDISPATCH WhichView, LPDISPATCH WhichWindow, short Button, long Shift, long X, long Y, IMSI_BOOL* pbCancel)
  424. {
  425.     *pbCancel = FALSE;
  426.     HRESULT hRes = E_FAIL;
  427.     CMerge merge;
  428.     IDrawing *pIDwgTrg = NULL;
  429.     Graphics *pGrs = NULL;
  430.     COleVariant varOptional(varMissing);
  431.  
  432.     CViewWnd *pView = (CViewWnd *)GetDlgItem(IDC_PREVIEW);
  433.  
  434.     AFX_MANAGE_STATE(m_pTool->m_pTCState);
  435.  
  436.     try
  437.     {
  438.         if(m_pDragGraphic != 0)
  439.         {
  440.  
  441.             pIDwgTrg = (IDrawing*)WhichDrawing;
  442.  
  443.             pIDwgTrg->AddRef();
  444.     
  445.             
  446.             hRes = merge.InitA(pView->m_pPreviewDrawing, pIDwgTrg);
  447.             CHECK_HRESULT(hRes)
  448.  
  449.             hRes = merge.RunA();
  450.             CHECK_HRESULT(hRes)
  451.  
  452.             hRes = pIDwgTrg->get_Graphics(&pGrs); // get current drawing graphic collection
  453.             CHECK_HRESULT(hRes)
  454.     
  455.             hRes = pGrs->AddGraphic(m_pDragGraphic, varOptional, varOptional);
  456.             CHECK_HRESULT(hRes)
  457.             
  458.             hRes = merge.CorrectGraphic(m_pDragGraphic);
  459.             CHECK_HRESULT(hRes)
  460.  
  461.             hRes = m_pDragGraphic->Update();
  462.             CHECK_HRESULT(hRes)
  463.  
  464.             pIDwgTrg->Release();
  465.             pIDwgTrg = NULL;
  466.  
  467.                     
  468.             CPoint point;
  469.             point.x = X;
  470.             point.y = Y;
  471.  
  472.             m_pView = (View*) WhichView;
  473.             m_pView->AddRef();
  474.  
  475.             EndDrag(point);
  476.  
  477.             hRes = m_pView->Refresh();
  478.             CHECK_HRESULT(hRes)
  479.  
  480.             m_FirstClick = FALSE;    
  481.         }
  482.     }
  483.     catch (...)
  484.     {
  485.         _clearfp();
  486.  
  487.         TRACE_EXCEPTION("TDialog::MouseUp")
  488.     }
  489.  
  490.     if (m_pView != NULL)
  491.     {
  492.         m_pView->Release();
  493.         m_pView = NULL;
  494.     }
  495.     if (pGrs != NULL)
  496.     {
  497.         pGrs->Release();
  498.         pGrs = NULL;
  499.     }
  500.  
  501.  
  502. }
  503. void CTDialog::MouseMove(LPDISPATCH WhichDrawing, LPDISPATCH WhichView, LPDISPATCH WhichWindow, long Shift, long X, long Y, IMSI_BOOL* pbCancel)
  504. {
  505.     *pbCancel = FALSE;
  506.  
  507.  
  508.     CPoint point;
  509.     point.x = X;
  510.     point.y = Y;
  511.     Graphics* GrsTmp = NULL;;
  512.     Graphics* pPreviewGrs = NULL;;
  513.     IGraphic* Gr1 = NULL;
  514.     long c = 0;
  515.     IDrawing *pIDwgTrg = NULL;
  516.     Graphics *pGrs = NULL;
  517.     View *pViewCur = NULL;
  518.  
  519.     try 
  520.     {
  521.  
  522.         if (m_FirstClick == TRUE)/// && m_bDragging != FALSE) 
  523.         {
  524.             
  525.             if(m_pView != NULL)
  526.             {
  527.                 m_pView->Release();
  528.             }
  529.             m_pView = (View *)WhichView;
  530.             m_pView->AddRef();
  531.  
  532.             CViewWnd *pPreview = (CViewWnd *) (GetDlgItem(IDC_PREVIEW));
  533.             
  534.             COleVariant varItem = (1L);
  535.             
  536.             HRESULT hRes = pPreview->m_pPreviewDrawing->get_Graphics(&pPreviewGrs);
  537.             CHECK_HRESULT(hRes)
  538.  
  539. #ifdef _DEBUG
  540.             hRes = pPreviewGrs->get_Count(&c);
  541.             CHECK_HRESULT(hRes)
  542. #endif
  543.  
  544.             varItem = 0L;
  545.  
  546.             hRes = pPreviewGrs->get_Item(varItem, &Gr1);
  547.             CHECK_HRESULT(hRes)
  548. // check if the graphic that we drag is empty            
  549.             hRes = Gr1->get_Graphics(&GrsTmp);
  550.             CHECK_HRESULT(hRes)
  551.  
  552.             long b = 0;
  553.             hRes = GrsTmp->get_Count(&b);
  554.             CHECK_HRESULT(hRes)
  555.             
  556.             if (b == 0) //if it is empty then return;
  557.             {
  558.                 
  559.                 GrsTmp->Release();
  560.                 GrsTmp = NULL;
  561.  
  562.                 Gr1->Release();
  563.                 Gr1 = NULL;
  564.  
  565.                 pPreviewGrs->Release();
  566.                 pPreviewGrs = NULL;
  567.  
  568.                 m_FirstClick = TRUE;    
  569.                 return;
  570.             }
  571.  
  572.  
  573.             hRes = Gr1->Duplicate(&m_pDragGraphic);
  574.             CHECK_HRESULT(hRes)
  575.             
  576.             m_pDragGraphic->Release();
  577.             m_pDragGraphic = NULL;
  578.     
  579.             hRes = pPreviewGrs->get_Count(&c);
  580.             CHECK_HRESULT(hRes)
  581.             varItem = c -1L;
  582.             hRes = pPreviewGrs->Remove(&varItem, &m_pDragGraphic);
  583.             CHECK_HRESULT(hRes)
  584.             m_FirstClick = FALSE;
  585.             BeginDrag(point);
  586.         }
  587.         else 
  588.         {
  589.             if (m_pDragGraphic != NULL && !m_bDragging)/// aalb && (nFlags & MK_LBUTTON))
  590.             {
  591.                 if (!BeginDrag(point))
  592.                 {
  593.                     m_pDragGraphic->Release();
  594.                     m_pDragGraphic = NULL;
  595.                 }
  596.             }
  597.             else if (m_bDragging)
  598.             {
  599.  
  600.                 pViewCur = (View *)WhichView;
  601.                 pViewCur->AddRef();
  602.                 if (pViewCur != m_pView) // if view is changed then restart drag
  603.                 {
  604.                     m_pView = pViewCur;
  605.                     pViewCur->Release();
  606.                     pViewCur = NULL;
  607.                     RestartDrag(point);                
  608.  
  609.                 }
  610.  
  611.                 Drag(point);
  612.             }    
  613.             
  614.         }
  615.     }    
  616.     catch (...)
  617.     {
  618.         TRACE_EXCEPTION("CTDialog::OnMouseMove")
  619.         _clearfp();
  620.     }
  621.  
  622.     if(GrsTmp != NULL)
  623.     {
  624.         GrsTmp->Release();
  625.         GrsTmp = NULL;
  626.     }
  627.     if(Gr1 != NULL)
  628.     {
  629.         Gr1->Release();
  630.         Gr1 = NULL;
  631.     }
  632.     if(pPreviewGrs != NULL)
  633.     {
  634.         pPreviewGrs->Release();
  635.         pPreviewGrs = NULL;
  636.     }
  637.  
  638.  
  639. }
  640.  
  641. void CTDialog::ConnectEvents()
  642. {
  643.     HRESULT hRes = S_OK;
  644.  
  645.     if (m_dwEventConnection != 0)
  646.     {
  647. //        AfxMessageBox("Already connected.");
  648.         return;
  649.     }
  650.     
  651.     m_pIApplication = NULL;
  652.  
  653.     try
  654.     {
  655.         hRes = m_pThisTool->get_Application(&m_pIApplication);
  656.         CHECK_HRESULT(hRes)
  657.  
  658.         if (m_pIApplication == NULL)
  659.         {
  660.     //            AfxMessageBox("Can't get a TurboCAD application!");
  661.                 return;
  662.         }
  663.  
  664.         
  665.         IDispatch* pUnkEventSink = GetIDispatch(TRUE);
  666.  
  667.         UpdateData(TRUE);
  668.         COleVariant var((long)m_dwEventMask);
  669.         hRes = m_pIApplication->ConnectEvents(pUnkEventSink,
  670.             &var,
  671.             (long*)&m_dwEventConnection);
  672.         
  673.  
  674.         if (FAILED(hRes))
  675.         {
  676.     //        AfxMessageBox("Connection not established.");
  677.             return;
  678.         }
  679.     }
  680.     catch (...)
  681.     {
  682.         TRACE_EXCEPTION("CTDialog::ConnectEvents")
  683.         _clearfp();
  684.     }
  685.  
  686.  
  687. }
  688.  
  689. void CTDialog::DisconectEvents()
  690. {
  691.     if (m_pIApplication == NULL || m_dwEventConnection == 0)
  692.         return;
  693.  
  694.     try
  695.     {
  696.         HRESULT    hRes = m_pIApplication->DisconnectEvents(m_dwEventConnection);
  697.         if (FAILED(hRes))
  698.         {
  699.     //        AfxMessageBox("Connection not broken.");
  700.             return;
  701.         }
  702.         m_pIApplication->Release(); // possibility problem
  703.         m_pIApplication = NULL;
  704.  
  705.         m_dwEventConnection = 0;
  706.     }
  707.     catch (...)
  708.     {
  709.         TRACE_EXCEPTION("CTDialog::DisconectEvents")
  710.         _clearfp();
  711.     }
  712.  
  713.  
  714. }
  715.  
  716. BOOL CTDialog::BeginDrag(const CPoint& point)
  717. {
  718.     BOOL bRes = FALSE;
  719.     HRESULT hRes = E_FAIL;
  720.     BoundingBox* Box = NULL;
  721.     IVertex* vMax = NULL;
  722.     IVertex* vMin = NULL;
  723.     double x, y, x1, y1;
  724.  
  725.     if (m_pView == NULL)
  726.         return bRes;
  727.     try
  728.     {
  729.         VariantClear(&m_dragOutline);
  730.         SAFEARRAYBOUND bound;
  731.         bound.cElements = 0;
  732.         bound.lLbound = 0;
  733.         m_dragOutline.vt = VT_ARRAY|VT_R8;
  734.         m_dragOutline.parray = ::SafeArrayCreate(VT_R8, 1, &bound);
  735.         hRes = m_pDragGraphic->CreateDragOutline(&m_dragOutline);
  736.         CHECK_HRESULT(hRes);
  737.  
  738.         m_bDragging = TRUE;
  739.         m_pDragGraphic->put_Visible(FALSE);
  740.         VARIANT varState;
  741.         varState.vt = VT_I4;
  742.         varState.lVal = imsiDragBegin;
  743.  
  744.         hRes = m_pDragGraphic->CalcBoundingBox(NULL, &Box);
  745.         CHECK_HRESULT(hRes)
  746.  
  747.         hRes = Box->get_Max(&vMax);
  748.         CHECK_HRESULT(hRes)
  749.  
  750.         hRes = Box->get_Min(&vMin);
  751.         CHECK_HRESULT(hRes)
  752.  
  753.         hRes = vMax->get_X(&x);
  754.         CHECK_HRESULT(hRes)
  755.  
  756.         hRes = vMax->get_Y(&y);
  757.         CHECK_HRESULT(hRes)
  758.  
  759.         hRes = vMin->get_X(&x1);
  760.         CHECK_HRESULT(hRes)
  761.  
  762.         hRes = vMin->get_Y(&y1);
  763.         CHECK_HRESULT(hRes)
  764.  
  765.         x = x1 + (x - x1)/2;
  766.         y = y1 + (y - y1)/2;
  767.         hRes = m_pView->ViewToScreen(x, y, &x1, &y1, NULL);
  768.         CHECK_HRESULT(hRes)
  769.  
  770.         hRes = m_pView->DragOutline(&m_dragOutline, x1, y1, &varState);
  771.         CHECK_HRESULT(hRes)
  772.         
  773.         varState.lVal = imsiDragContinue;
  774.         hRes = m_pView->DragOutline(&m_dragOutline, point.x, point.y, &varState);
  775.         CHECK_HRESULT(hRes)
  776.  
  777.         bRes = TRUE;
  778.     }
  779.     catch (...)
  780.     {
  781.         TRACE_EXCEPTION("CTDialog::BeginDrag")
  782.         _clearfp();
  783.     }
  784.  
  785.     if (vMin != NULL)
  786.     {
  787.         vMin->Release();
  788.         vMin = NULL;
  789.     }
  790.     if (vMax != NULL)
  791.     {
  792.         vMax->Release();
  793.         vMax = NULL;
  794.     }
  795.     if (Box != NULL)
  796.     {
  797.         Box->Release();
  798.         Box = NULL;
  799.     }
  800.  
  801.     return bRes;
  802. }
  803.  
  804. BOOL CTDialog::Drag(const CPoint& point)
  805. {
  806.     BOOL bRes = FALSE;
  807.     if (m_pView == NULL)
  808.         return bRes;
  809.  
  810.     try
  811.     {
  812.         VARIANT varState;
  813.         varState.vt = VT_I4;
  814.         varState.lVal = imsiDragContinue;
  815.         HRESULT hRes = m_pView->DragOutline(&m_dragOutline, point.x, point.y, &varState);
  816.         CHECK_HRESULT(hRes)
  817.         bRes = TRUE;
  818.     }
  819.     catch (...)
  820.     {
  821.         TRACE_EXCEPTION("CTDialog::Drag")
  822.         _clearfp();
  823.     }
  824.     return bRes;
  825. }
  826.  
  827. BOOL CTDialog::EndDrag(const CPoint& point)
  828. {
  829.     BOOL bRes = FALSE;
  830.     m_bDragging = FALSE;
  831.     IMatrix *pMat = NULL;
  832.     if (m_pView == NULL || m_pDragGraphic == NULL)
  833.         return bRes;
  834.  
  835.  
  836.     IDrawing *pIDwg = NULL;
  837.     Properties *pProps = NULL;
  838.     Property *pProp = NULL;
  839.  
  840.     try
  841.     {
  842.         VARIANT varState;
  843.         varState.vt = VT_I4;
  844.         varState.lVal = imsiDragEnd;
  845.         HRESULT hRes = m_pView->DragOutline(&m_dragOutline, point.x, point.y, &varState);
  846.         CHECK_HRESULT(hRes)
  847.  
  848.         VariantClear(&m_dragOutline);
  849.  
  850.         double dx, dy, dz;
  851.         dx = dy = dz = 0;
  852.  
  853.         double dxWorld, dyWorld, dzWorld;
  854.         dxWorld = dyWorld = dzWorld = 0;
  855.  
  856.         hRes = m_pView->ScreenToView(point.x, point.y, &dx, &dy);
  857.         CHECK_HRESULT(hRes)
  858.  
  859.         dxWorld = dx;
  860.         dyWorld = dy; 
  861.  
  862.         VARIANT var;
  863.         var.vt = VT_ERROR;
  864.         var.scode = DISP_E_PARAMNOTFOUND;
  865. //        ImsiSpaceModeType curMode;    
  866. // will be available in TCW5.03 decoment this part to give correct position of inserted symbol in model space.
  867.  
  868.         //    HRESULT hRes = S_OK;
  869.         CString cstrName("TileMode");//# Non-localizable string#
  870.         BSTR bstrName = cstrName.AllocSysString();
  871.  
  872.         VARIANT varItem;
  873.       ::VariantInit(&varItem);
  874.         varItem.vt = VT_BSTR;
  875.         varItem.bstrVal = bstrName;
  876.         hRes = m_pView->get_Drawing(&pIDwg);
  877.         CHECK_HRESULT(hRes)
  878.         
  879.         hRes = pIDwg->get_Properties(&pProps);
  880.         CHECK_HRESULT(hRes)
  881.  
  882.         hRes = pProps->get_Item(&varItem, &pProp);
  883.         CHECK_HRESULT(hRes)
  884.  
  885.         COleVariant varVal;//(0L);
  886.         hRes = pProp->get_Value(NULL, &varVal);
  887.         CHECK_HRESULT(hRes)
  888.  
  889.         if(varVal.lVal == 1) //ModelSpace
  890.         {
  891.             hRes = m_pView->ViewToWorld(dx, dy, dz, &dxWorld, &dyWorld, &dzWorld);
  892.             CHECK_HRESULT(hRes)
  893.         }
  894.  
  895.         hRes = m_pDragGraphic->MoveAbsolute(dxWorld, dyWorld, 0, &var, &var, &var, &pMat);
  896.         CHECK_HRESULT(hRes)
  897.  
  898.         hRes = m_pDragGraphic->put_Visible(TRUE);
  899.         CHECK_HRESULT(hRes)
  900.  
  901.         bRes = TRUE;
  902.     }
  903.     catch (...)
  904.     {
  905.         TRACE_EXCEPTION("CTDialog::EndDrag")
  906.         _clearfp();
  907.     }
  908.  
  909.     if(pMat != NULL)
  910.     {
  911.         pMat->Release();
  912.         pMat = NULL;
  913.     }
  914.     
  915.     if (m_pDragGraphic != NULL)
  916.     {
  917.         m_pDragGraphic->Release();
  918.         m_pDragGraphic = NULL;
  919.     }
  920.  
  921.     if (pProp != NULL)
  922.     {
  923.         pProp->Release();
  924.         pProp = NULL;
  925.     }
  926.  
  927.     if (pProps != NULL)
  928.     {
  929.         pProps->Release();
  930.         pProps = NULL;
  931.     }
  932.     if (pIDwg != NULL)
  933.     {
  934.         pIDwg->Release();
  935.         pIDwg = NULL;
  936.     }
  937.  
  938.     return bRes;
  939. }
  940.  
  941. BOOL CTDialog::RestartDrag(const CPoint& point)
  942. {
  943.     try
  944.     {
  945.         m_bDragging = FALSE;
  946.         VARIANT varState;
  947.         varState.vt = VT_I4;
  948.         varState.lVal = imsiDragEnd;
  949.         HRESULT hRes = m_pView->DragOutline(&m_dragOutline, point.x, point.y, &varState);
  950.         CHECK_HRESULT(hRes)
  951.  
  952.         VariantClear(&m_dragOutline);
  953.         BeginDrag(point);
  954.     }
  955.     catch (...)
  956.     {
  957.         TRACE_EXCEPTION("CTDialog::RestartDrag")
  958.         _clearfp();
  959.     }
  960.  
  961.     return TRUE;
  962. }
  963.  
  964. void CTDialog::OnBrowse()
  965. {
  966.  
  967.     CToolList *pList = (CToolList *)GetDlgItem(IDC_LIST1);
  968.     CViewWnd *pView = (CViewWnd *)GetDlgItem(IDC_PREVIEW);
  969.     CEdit *pPath = (CEdit *) GetDlgItem(IDC_EDITPATH);
  970.  
  971.     
  972.     CDirDialog* pDirDlg = new CDirDialog;
  973.     //pDirDlg->m_strTitle = "Select directory contains tcw symbols. ";//# Localizable string#
  974.     pDirDlg->m_strTitle.LoadString(IDS_STRING104);
  975.     pPath->GetWindowText(pDirDlg->m_strSelDir);     
  976.     CString strtmp;
  977.     pPath->GetWindowText(strtmp); 
  978.     int i = pDirDlg->DoBrowse (m_hWnd); 
  979.     if(i == 0)
  980.         return;
  981.     
  982.     HCURSOR hOC = ::SetCursor(::LoadCursor(NULL,IDC_WAIT));
  983.  
  984.     if (strtmp != pDirDlg->m_strPath)
  985.     {
  986.  
  987.         pPath->SetWindowText ((LPCTSTR) pDirDlg->m_strPath);
  988.         pView->ClearAll ();
  989.         pView->Invalidate ();
  990.         pList->RefreshFileList(pDirDlg->m_strPath);
  991.         if(pList->SetCurSel(0) != LB_ERR)
  992.         {
  993.  
  994.             pList->GetText(0 ,(pList->m_cstrCurSelected));
  995.             pView->CreatePreview();
  996.             pView->DoPreview();            
  997.         
  998.         }
  999. //        else
  1000. //        {
  1001. //            pList->m_cstrCurSelected = "";
  1002. //        }
  1003.     }
  1004.     ::SetCursor(hOC);
  1005.  
  1006. }
  1007.